<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改门禁设备天线' : '新增门禁设备天线'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" label-width="140px" :rules="rules">
      <el-form-item label="门禁设备名称" prop="accessName">
        <el-input v-model="form.accessName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="门禁设备天线名称" prop="antennaName">
        <el-input v-model.number="form.antennaName" placeholder="门禁设备天线名称" />
      </el-form-item>
      <el-form-item label="天线序号" prop="antennaNo">
        <el-input type="number" v-model.number="form.antennaNo" placeholder="请输入天线序号" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { accessAntennaCreate, accessAntennaUpdate } from '@/api/assetsNew'
import { openNotification } from '@/utils/util'
export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      form: {
        accessName: null,
        antennaName: null,
        antennaNo: null
      },
      rules: {
        accessName: [{ required: true, message: '请输入门禁设备名称', trigger: 'blur' }],
        antennaName: [{ required: true, message: '请输入门禁设备天线名称', trigger: 'blur' }],
        antennaNo: [
          { required: true, message: '请输入天线序号', trigger: 'blur' },
          { type: 'number', message: '请输入数字' }
        ]
      }
    }
  },
  methods: {
    edit(bool, row) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, row)
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await accessAntennaUpdate(this.form)
            // 修改
          } else {
            // 新增
            mes = '新增操作成功'
            res = await accessAntennaCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
